import { defineStore } from 'pinia'
import $ajax from '@/api/to8030.js'
import { showErrorMsg } from '@/utils/alterMessage.js'

export const useMemberStore = defineStore('member', {
  state: () => ({
    /** @type{memId:number,memNumber:string,memName:string,memPwd:string,memSex:string,memBirthday:string,provinceId:number,cityId:number,address:string,roleId:number,memState:number,verifyCode:string,remarks:string} */
    memberList: [],
    /** @type{memId:number,memNumber:string,memName:string,memPwd:string,memSex:string,memBirthday:string,provinceId:number,cityId:number,address:string,roleId:number,memState:number,verifyCode:string,remarks:string} */
    currentMember: {},
    pageInfo: {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      pageSizes: [5, 10, 20, 30, 40, 50]
    },
    dialogFormVisible: false,
    drawerVisible: false
  }),
  actions: {
    init() {
      this.pageInfo = {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        pageSizes: [5, 10, 20, 30, 40, 50]
      }
    },
    setMember(member) {
      this.member = member
    },
    setCurrentMember(member) {
      this.currentMember = member
    },
    async getMemberList() {
      const path = '/member/select/page'
      const res = await $ajax.post(path, this.pageInfo)
      if (res !== null && res.code != null && res.code === 200) {
        this.memberList = res.result.records
        this.pageInfo.total = res.result.total
        this.pageInfo.currentPage = res.result.current
        this.pageInfo.pageSize = res.result.size
      } else {
        showErrorMsg('获取成员列表失败')
      }
    },
    async deleteMember(memId) {
      const path = '/member/delete'
      const res = await $ajax.delete(path, {
        params: {
          memId
        }
      })
      if (res !== null && res.code != null && res.code === 200) {
        location.reload()
      } else {
        showErrorMsg('删除成员失败')
      }
    },
    async updateMember(memId, roleId, workProvince) {
      // console.log(memId + '   ' + roleId)
      const path = '/member/update'
      const res = await $ajax.put(path, {
        memId,
        roleId,
        workProvince
      })
      if (res !== null && res.code != null && res.code === 200) {
        return true
      } else {
        showErrorMsg('更新人员职位失败')
        return false
      }
    },
    async getGridList() {
      const path = '/member/select/grid'
      const res = await $ajax.get(path)
      if (res !== null && res.code != null && res.code === 200) {
        this.memberList = res.result
      } else {
        showErrorMsg('获取网格员列表失败')
      }
    },
    async getGridPage() {
      const path = '/member/select/gridPage'
      const res = await $ajax.post(path, this.pageInfo)
      if (res !== null && res.code != null && res.code === 200) {
        this.memberList = res.result.records
        this.pageInfo.total = res.result.total
        this.pageInfo.currentPage = res.result.current
        this.pageInfo.pageSize = res.result.size
      } else {
        showErrorMsg('获取网格员列表失败')
      }
    }
  }
})
